const recorderManager = wx.getRecorderManager()
const backgroundAudio = wx.getBackgroundAudioManager()
// yb_mingpian/pages/tests/tests.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openRecordingdis: "block", //显示录机图标
    shutRecordingdis: "none", //隐藏停止图标
    recordingTimeqwe: 0, //录音计时
    setInter: "", //录音名称
    soundUrl: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  aaa() {
    console.log('按下事件')
  },
  bbb() {
    console.log('手指离开事件')
  },

  //录音计时器
  recordingTimer: function () {
    var that = this;
    //将计时器赋值给setInter
    that.data.setInter = setInterval(
      function () {
        var time = that.data.recordingTimeqwe + 1;
        that.setData({
          recordingTimeqwe: time
        })
      }, 1000);
  },
  //开始录音
  openRecording: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          shutRecordingdis: "block",
          openRecordingdis: "none"
        })
      }
    })
    const options = {
      duration: 60000, //指定录音的时长，单位 ms，最大为10分钟（600000），默认为1分钟（60000）
      sampleRate: 16000, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 96000, //编码码率
      format: 'mp3', //音频格式，有效值 aac/mp3
      frameSize: 50, //指定帧大小，单位 KB
    }
    //开始录音计时   
    that.recordingTimer();
    //开始录音
    recorderManager.start(options);
    recorderManager.onStart(() => {
      console.log('。。。开始录音。。。')
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log(res);
    })
  },

  //结束录音
  shutRecording: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          shutRecordingdis: "none",
          openRecordingdis: "block"
        })
      }
    })
    recorderManager.stop();
    recorderManager.onStop((res) => {
      const that = this
      // let timestamp = util.formatTime2(new Date());
      console.log('。。停止录音。。', res.tempFilePath)
      const {
        tempFilePath
      } = res;
      //结束录音计时  
      clearInterval(that.data.setInter);
      //上传录音
      wx.uploadFile({
        url: appURL + '/wx_SubjectInformation/wx_SubjectRecordKeeping.do',//这是你自己后台的连接
        filePath: tempFilePath,
        name: "file",//后台要绑定的名称
        header: {
          "Content-Type": "multipart/form-data"
        },
        //参数绑定
        formData: {
          recordingtime: that.data.recordingTimeqwe,
          topicid: that.data.topicid,
          userid: 1,
          praisepoints: 0
        },
        success: function (ress) {
          console.log(res);
          wx.showToast({
            title: '保存完成',
            icon: 'success',
            duration: 2000
          })
        },
        fail: function (ress) {
          console.log("。。录音保存失败。。");
        }
      })


      // wx.cloud.uploadFile({
      //   cloudPath: "sounds/" + timestamp + '-' + this.randomNum(10000, 99999) + '.mp3',
      //   filePath: tempFilePath,
      //   // 成功回调
      //   success: res => {
      //     console.log('上传成功', res)
      //     that.setData({
      //       soundUrl: res.fileID,
      //       // time: util.formatTime1(new Date())
      //     })
      //   },
      // })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})